package com.lemon;


import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;

/**
 * @author 软柠柠吖
 * @date 2022/1/25
 */
public class Test1 {
    ArrayList<Integer> list = new ArrayList<>();

    @Before
    public void init() {
        list.add(1);
        list.add(3);
        list.add(3);
        list.add(3);
        list.add(3);
        list.add(4);
    }

    @Test
    public void test1() {
        System.out.println(list);
        for (int i = 0; i < list.size(); i++) {
//            if (list.get(i).equals(3)) {
//                list.remove(i);
//            }
            if (list.get(i) == 3) {
                list.remove(i);
            }
        }
        System.out.println(list);
    }

    @Test
    public void test2() {
        System.out.println(list);
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            Integer integer = iterator.next();
            if (integer == 3) {
                list.remove(integer);
                //list.remove(integer);
                iterator.remove();
            }
        }
        System.out.println(list);
    }

    @Test
    public void test3() {
        System.out.println(list);
        for (Integer integer : list) {
            if (integer == 3) {
                list.remove(integer);
                continue;
            }
        }
        System.out.println(list);
    }


// ----------------------------正确代码-------------------------

    @Test
    public void test4() {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == 3) {
                list.remove(i);
                // 因为ArrayList 是动态数组，当调用remove方法删除元素时，后面的元素会 自动前移(自动的调整位置)
                // , 而我们的指针 i进行 i++，指针会后移，导致删除一个元素后会 漏掉下一个元素，因此我们需要指针回拨的操作
                i--;// 指针回拨

            }
        }
    }

    @Test
    public void test5() {
        System.out.println(list);
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            Integer integer = iterator.next();
            if (integer.equals(3)) {
                iterator.remove();
            }
        }
        System.out.println(list);
    }

    //  public E remove(int index) {
    //      rangeCheck(index);
    //
    //      modCount++;
    //      E oldValue = elementData(index);
    //
    //      int numMoved = size - index - 1;
    //      if (numMoved > 0)
    //          System.arraycopy(elementData, index+1, elementData, index,
    //                          numMoved);
    //      elementData[--size] = null; // clear to let GC do its work
    //
    //      return oldValue;
    //  }
//  final void checkForComodification() {
//      if (modCount != expectedModCount)
//      throw new ConcurrentModificationException();
//  }
    @Test
    public void test6() {
        Object[] a = {1, 2, 5, 8};
        int index = 1;

        System.arraycopy(a, index + 1, a, index, a.length - index - 1);
        //a[a.length-1] = null;
        System.out.println(Arrays.toString(a));
    }





/*

        public E remove(int index) {
            rangeCheck(index);

            modCount++;
            E oldValue = elementData(index);

            int numMoved = size - index - 1;
            if (numMoved > 0)
                System.arraycopy(elementData, index+1, elementData, index,
                                 numMoved);
            elementData[--size] = null; // clear to let GC do its work

            return oldValue;
        }


        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }

        public E next() {
            checkForComodification();
            int i = cursor;
            Object[] elementData = ArrayList.this.elementData;
            cursor = i + 1;
            return (E) elementData[lastRet = i];
        }

        public void remove() {
            checkForComodification();

            try {
                ArrayList.this.remove(lastRet);
                cursor = lastRet;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }
 */

    @Test
    public void test7() {
        CopyOnWriteArrayList<Object> copyOnWriteArrayList = new CopyOnWriteArrayList<>();
        Iterator<Object> iterator = copyOnWriteArrayList.iterator();
        while (iterator.hasNext()) {
            Object obj =  iterator.next();

        }
        System.out.println(list);
        for (Integer integer : list) {


            if (integer == 3) {
                list.remove(integer);
                continue;
            }
        }
        System.out.println(list);
    }

}
